--精品分类商品

local temp_price_key="temp_price";
local tempProductList ="temp:product_id:"
local all_id={};
local total=0
local list2={}
local list3={};
redis.log(redis.LOG_NOTICE, "product query start... ");
--分页函数
local getPageIndex = function(x)
    local page = {}
    local startIndex=ARGV[1]
    local endIndex=ARGV[1]+ARGV[2]-1
    if KEYS[3]=="ASC" then
        startIndex=-ARGV[1]-ARGV[2]
        endIndex=-ARGV[1]-1
    end
    redis.log(redis.LOG_NOTICE, " query  product start:"..startIndex.."end:"..endIndex);
    table.insert(page,startIndex)
    table.insert(page,endIndex)
    return page
end
local not_empty = function(x)
    return (type(x) == "table") and (not x.err) and (#x ~= 0)
end
--价格过滤函数
local filter_price = function(temp_price_key,price_key,min,max)
    redis.log(redis.LOG_NOTICE, "filter_price start 1 ... ");
    local price_list=redis.call("ZREVRANGEBYSCORE",price_key,max,min)
    local i=1;
    while (i<=#price_list) do
        redis.call("ZADD",temp_price_key,0,price_list[i])
        if i>100000 then
            break;
        end
        i=i+1
    end
    redis.log(redis.LOG_NOTICE, "filter_price end max "..max.." min"..min);

end
--传入字符串和分隔符，返回分割后的table
function string.split(str, delimiter,prefix)
    local result = {}
    if str==nil or str=='' or delimiter==nil then
        return nil
    end
    string.gsub(str, '[^'..delimiter..']+', function(w) table.insert(result, prefix..w) end )
    return result
end
--KEYS[2]是分数索引 用于集合排序运算
if #KEYS==5 then
    --只查询分类或只查询品牌
    local values=string.split(ARGV[3],",",KEYS[5])
    local values_size_t = table.getn(values)
    if values_size_t==1 then
        if #ARGV==5 then
            --筛选价格
            filter_price(temp_price_key,values[1],ARGV[4],ARGV[5])
            --与索引取交集
            redis.call("ZINTERSTORE", tempProductList,2,temp_price_key,KEYS[2],"AGGREGATE","MAX")
        else
            --与索引取交集
            redis.call("ZINTERSTORE", tempProductList,2,values[1],KEYS[2],"AGGREGATE","MAX")
        end
    elseif values_size_t>1 then
        local tempCategory="temp:category"
        redis.call("ZUNIONSTORE",tempCategory,values_size_t,unpack(values))
        if #ARGV==5 then
            filter_price(temp_price_key,tempCategory,ARGV[4],ARGV[5])
            redis.call("ZINTERSTORE", tempProductList,2,temp_price_key,KEYS[2],"AGGREGATE","MAX")
        else
            redis.call("ZINTERSTORE", tempProductList,2,tempCategory,KEYS[2],"AGGREGATE","MAX")
        end

        redis.call("DEL", tempCategory)
    end

    total=redis.call("ZCARD", tempProductList);
    local page= getPageIndex(total)
    all_id =redis.call("ZREVRANGE", tempProductList,page[1],page[2]);
elseif #KEYS==6 then
    --查询分类与品牌
    local category_values=string.split(ARGV[3],",",KEYS[5])
    local category_size_t = table.getn(category_values)
    local brand_values=string.split(ARGV[4],",",KEYS[6])
    local brand_size_t = table.getn(brand_values)
    --先与再或
    local tempList={}
    local i=category_size_t;

    while(1<=i) do
        local j=brand_size_t;
        while(1<=j) do
            local str1="temp"..i..j
            redis.call("ZINTERSTORE",str1,2,category_values[i],brand_values[j],"AGGREGATE","MAX")
            table.insert(tempList,str1);
            j=j-1
        end
        i=i-1
    end

    local list_size=table.getn(tempList);
    if list_size>1 then
        local unionResult="tempUnion"
        table.insert(tempList,"AGGREGATE");
        table.insert(tempList,"MAX");
        redis.call("ZUNIONSTORE",unionResult,list_size,unpack(tempList))
        if  #ARGV==6 then
            --筛选价格 ARGV[5] 最低价格，ARGV[6] 最高价格
            filter_price(temp_price_key,unionResult,ARGV[5],ARGV[6])
            redis.call("ZINTERSTORE", tempProductList,2,temp_price_key,KEYS[2],"AGGREGATE","MAX")
        else
            redis.call("ZINTERSTORE", tempProductList,2,unionResult,KEYS[2],"AGGREGATE","MAX")
        end
        redis.call("DEL", unionResult)
    elseif list_size==1 then
        if  #ARGV==6 then
            filter_price(temp_price_key,tempList[1],ARGV[5],ARGV[6])
            redis.call("ZINTERSTORE", tempProductList,2,temp_price_key,KEYS[2],"AGGREGATE","MAX")
        else
            redis.call("ZINTERSTORE", tempProductList,2,tempList[1],KEYS[2],"AGGREGATE","MAX")
        end
    end
    --删除临时集合
    for _, key in ipairs(tempList) do
        redis.call("DEL", key);
    end

    total=redis.call("ZCARD", tempProductList);
    local page= getPageIndex(total)
    all_id =redis.call("ZREVRANGE", tempProductList,page[1],page[2]);
else
    --不需价格过滤
    total=redis.call("ZCARD",KEYS[2]);
    local page= getPageIndex(total)
    all_id =redis.call("ZREVRANGE",KEYS[2],page[1],page[2]);
end
redis.call("DEL", temp_price_key)
redis.call("DEL", tempProductList)
--获取产品对象
if not_empty(all_id) then
    local i=1
    while(i<=#all_id) do
        local product=redis.call("GET",KEYS[1].. all_id[i]);
        if product~=nil then
            if KEYS[3]=="ASC" then
                table.insert(list2,1,product);
            else
                table.insert(list2,product);
            end
        end
        i=i+1
    end
end


--取优惠券
if not_empty(list2) then
    for _,v in ipairs(list2) do
        if type(v)=="string" then
            local obj= cjson.decode(v)
            local couponList=redis.call("GET",KEYS[4]..obj.brandCode);

            if couponList~=nil and type(couponList)=="string" then
                obj.coupons=cjson.decode(couponList);
                table.insert(list3,cjson.encode(obj));
            else
                table.insert(list3,v);
            end
        end
    end
end
redis.log(redis.LOG_NOTICE, "product query finish! total :"..total);
return {total,list3}
